| Conditions | 1 |
| Paths | 1 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var chai = require('chai'); |
||
| 15 | describe('User.manager', function() { |
||
| 16 | before( function() { |
||
| 17 | config.users.enable = true |
||
| 18 | this.fixture = { |
||
| 19 | htmlIsAuthorized: fs.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'isAuthorized.html'), 'utf8'), |
||
| 20 | htmlIsAuthorizedTrue: fs.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'isAuthorizedTrue.html'), 'utf8'), |
||
| 21 | users: JSON.parse(fs.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'users', 'users.json'), 'utf8')) |
||
| 22 | } |
||
| 23 | }); |
||
| 24 | |||
| 25 | it('User.manager.instance.get', function(){ |
||
| 26 | // stub |
||
| 27 | var sinonInstance = sinon.sandbox.create(); |
||
| 28 | var stubRead = sinonInstance.stub(User.manager.instance, 'read'); |
||
| 29 | stubRead.returns(JSON.parse(JSON.stringify(this.fixture.users))) |
||
| 30 | |||
| 31 | // test |
||
| 32 | var res = User.manager.instance.get() |
||
| 33 | chai.expect(res.length).to.be.above(0) |
||
| 34 | |||
| 35 | // unstub |
||
| 36 | User.manager.instance.read.restore() |
||
| 37 | }) |
||
| 38 | |||
| 39 | it('User.manager.instance.save', function(){ |
||
| 40 | // stub |
||
| 41 | var sinonInstance = sinon.sandbox.create(); |
||
| 42 | var stubFs = sinonInstance.stub(fs, 'writeJsonSync'); |
||
| 43 | stubFs.returns(null) |
||
| 44 | |||
| 45 | // test |
||
| 46 | var res = User.manager.instance.save() |
||
|
|
|||
| 47 | |||
| 48 | // unstub |
||
| 49 | sinon.assert.calledOnce(fs.writeJsonSync) |
||
| 50 | fs.writeJsonSync.restore() |
||
| 51 | }) |
||
| 52 | |||
| 53 | it('User.manager.instance.read', function(){ |
||
| 54 | // stub |
||
| 55 | var sinonInstance = sinon.sandbox.create(); |
||
| 56 | var stubFs = sinonInstance.stub(fs, 'readFileSync'); |
||
| 57 | stubFs.returns(JSON.stringify(this.fixture.users)) |
||
| 58 | var stubExist = sinonInstance.stub(coreUtils.file, 'exist'); |
||
| 59 | stubExist.returns(true) |
||
| 60 | |||
| 61 | // test |
||
| 62 | var res = User.manager.instance.read() |
||
| 63 | chai.expect(res).to.not.be.null |
||
| 64 | |||
| 65 | // unstub |
||
| 66 | sinon.assert.calledOnce(fs.readFileSync) |
||
| 67 | fs.readFileSync.restore() |
||
| 68 | sinon.assert.calledOnce(coreUtils.file.exist) |
||
| 69 | coreUtils.file.exist.restore() |
||
| 70 | }) |
||
| 71 | |||
| 72 | it('User.manager.instance.update', function(){ |
||
| 73 | // stub |
||
| 74 | var sinonInstance = sinon.sandbox.create(); |
||
| 75 | var stubSave = sinonInstance.stub(User.manager.instance, 'save'); |
||
| 76 | stubSave.returns(JSON.parse(JSON.stringify(this.fixture.users))) |
||
| 77 | |||
| 78 | // test |
||
| 79 | var res = User.manager.instance.update() |
||
| 80 | chai.expect(res).to.be.equal(true) |
||
| 81 | |||
| 82 | // unstub |
||
| 83 | sinon.assert.calledOnce(User.manager.instance.save) |
||
| 84 | User.manager.instance.save.restore() |
||
| 85 | }) |
||
| 86 | }); |